// pages/mine/mine.js
const app = getApp();
var util = require("../../utils/util.js")
const db = wx.cloud.database()
const _ = db.command
const userinfoDB=db.collection('userInfo')
// const userMessageDB=db.collection('userMessage')
var userOpenid = app.globalData.userOpenid

Page({

  /**
   * 页面的初始数据
   */
  data: {
    _id:app.globalData._id,
    head_img:app.globalData.head_img,
    nickname:app.globalData.nickname,
    academy:util.AcademybyNum(app.globalData.academy),
    // userOpenid: app.globalData.userOpenid,
    login_hidden:app.globalData.login_hidden,
    btn_opacity:0.5,
    menuitems: [
      { text: '我的公告', url: '../mine/myPublish/myPublish', icon: '/images/pub.png', tips: '', arrows: '/images/arrows.png' },
      { text: '我的留言', url: '../mine/myMessage/myMessage', icon: '/images/message.png', tips: '', arrows: '/images/arrows.png' },
      { text: '个人设置', url: '../mine/setting/setting', icon: '/images/setting.png', tips: '', arrows: '/images/arrows.png' },
      { text: '帮助说明', url: '../mine/about/about', icon: '/images/help.png', tips: '', arrows: '/images/arrows.png' }
    ],
    hasUserInfo: false,
    _isManager:false,
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  //点击登录按钮
  getUserInfomation:function(e){
    console.log(e)
    let that = this
    wx.showModal({
      title: '授权',
      content: '我们需要获取你的一些信息',
      confirmText:'同意',
      cancelText:'拒绝',
      success (res) {
        if (res.confirm) {
          console.log(e.detail.userInfo) 
          wx.cloud.callFunction({
            name:'getOpenid',
            success:res=>{              
              console.log('openid--',res.result.openid)
              // that.setData({
              //   userOpenid:res.result.openid
              // })
              let gettedopenid = res.result.openid
              userinfoDB.where({
                _openid:gettedopenid
              }).get().then(res => {
                console.log(res.data[0])
                let data = res.data[0]
                if(data==null){
                  console.log("未找到用户")
                  let userinfo = e.detail.userInfo
                  // that.createMyMessage(gettedopenid)
                  userinfoDB.add({
                    data:{
                      userAvatar: userinfo.avatarUrl,
                      userLabel:100,
                      userNickname: userinfo.nickName,
                      isManager:false
                    },success:function(res){
                        console.log("添加数据成功")
                        app.globalData={
                          head_img:userinfo.avatarUrl,
                          nickname:userinfo.nickName,
                          academy:100,
                          login_hidden:true,
                          userOpenid:gettedopenid,
                          isManager:false
                        }   
                        console.log("全局变量：",app.globalData)     
                     
                        that.setData({
                          head_img:app.globalData.head_img,
                          nickname:app.globalData.nickname,
                          // userOpenid: app.globalData.userOpenid,
                          login_hidden:app.globalData.login_hidden,
                          _isManager:app.globalData.isManager,
                          hasUserInfo:true
                        })
                        userOpenid = app.globalData.userOpenid
                    },fail:function(res){
                      wx.showToast({
                        title: '系统出错',
                      })
                    }
                  })
                }else{
                  console.log("找到用户")
                  app.globalData={
                    head_img:data.userAvatar,
                    nickname:data.userNickname,
                    academy:data.userLabel,
                    login_hidden:true,
                    userOpenid:data._openid,
                    isManager:data.isManager
                  }

                  console.log("全局变量：",app.globalData)
                  that.setData({
                    head_img:app.globalData.head_img,
                    nickname:app.globalData.nickname,
                    academy:util.getAcademybyNum(app.globalData.academy),
                    // userOpenid: app.globalData.userOpenid,
                    login_hidden:app.globalData.login_hidden,
                    btn_opacity:data.userLabel>=26?0.5:1,
                    hasUserInfo: true,
                    _isManager:app.globalData.isManager
                  })
                  userOpenid = app.globalData.userOpenid
                }
              })
            },fail:res=>{
              wx.showToast({
                title: '未获取任何信息，请重试',
              })
            }
          })
      } else if (res.cancel) {
    console.log('用户点击取消')
   }
  }})
  },

  //创建我的留言数据库
  // createMyMessage:function(userOpenID){
    // userMessageDB.add({
    //   data:{
    //     userID:userOpenID,  
    //     message:[]
    //   },success:function(res){
    //     console.log(res)
    //     console.log("创建我的留言数据库成功！")
    //   }
    // })
  // },

  //选择学院
  selectAcademy:function(){
    if(this.data.login_hidden){
        wx.navigateTo({
          url: './selectAcademy/selectAcademy',
        })
    }else{
      wx.showToast({
        icon:"none",
        title: '请先登录',
      })
    }
  },

  navetoMymessage:function(e){
      if(app.globalData.userOpenid == null){
          wx.showToast({
            title: '登录后才能查看留言',
            icon:"none"
          })
      }else{
          wx.navigateTo({
            url: '../mine/myMessage/myMessage',
          })
      }
  },

  navetoMypublish:function(e){
    if(app.globalData.userOpenid == null){
      wx.showToast({
        title: '登录后才能查看公告',
        icon:"none"
      })
   }else{
      wx.navigateTo({
        url: '../mine/myPublish/myPublish',
      })
  }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示   实现动态监听更新数据
   */
  onShow: function () {
    this.setData({
      nickname:app.globalData.nickname,
      head_img:app.globalData.head_img
      })
  },
  setAcademyBtnTo:function(_academy){
    this.setData({
      btn_opacity:1,
      academy:util.getAcademybyNum(Number(_academy))
    })
  }
})